package com.catbad.controller;

import com.catbad.domain.User;
import com.catbad.services.FilmServices;
import com.catbad.services.OrderServices;
import com.catbad.services.UserServices;
import com.catbad.util.Tools;
import com.catbad.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class UserFilmController {
    @Autowired
    FilmServices filmServices;
    @Autowired
    UserServices userServices;
    @Autowired
    OrderServices orderServices;
    /**
     * home影片搜索
     * @param filmName
     * @param model
     * @return
     */
    @RequestMapping("homeFilmSearch")
    public String homeSearch(String filmName,Model model){
        model.addAttribute("filmList",filmServices.getFilmByName(filmName));
        model.addAttribute("search",filmName);
        return "user/home";
    }
    /**
     * home影片搜索
     * @param filmName
     * @param model
     * @return
     */
    @RequestMapping("listFilmSearch")
    public String listFilmSearch(String filmName,Model model){
        model.addAttribute("filmList",filmServices.getFilmByName(filmName));
        model.addAttribute("search",filmName);
        return "user/listFilm";
    }

    /**
     * 更多电影页面查询和分页
     * @param filmName
     * @param pageNO
     * @param model
     * @return
     */
    @RequestMapping("moreFilmPage")
    public String moreFilmPage(String filmName,Integer pageNO,Model model){
        model.addAttribute("moreFilmPageVO",filmServices.getMoreFilmPage(filmName,pageNO));
        return "user/listFilm";
    }
    /**
     * 影片详细信息
     * @param filmId
     * @param model
     * @return
     */
    @RequestMapping("filma")
    public String film(Long filmId,Model model){
        model.addAttribute("film",filmServices.getFilmMapById(filmId));
        return"user/film";
    }
    @RequestMapping("personal")
    public String personal(Integer pageNo,Integer rows,HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        OrderVO orderVO = null;
        if(user!=null){
            //传递订单信息
            orderVO = orderServices.getOrderPage(pageNo,rows,user.getUid());
        }else {
            orderVO = new OrderVO();
            orderVO.setMaxPage(1);
            orderVO.setTotal(0L);
            orderVO.setPageNo(1);
            orderVO.setRows(4);
        }
        model.addAttribute("orderVO",orderVO);
        return "user/personal";
    }


}
